package com.supermap.dataservice.file;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.file.FileReader;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author aqua
 * @description  弃用 转移Task
 * @date 2019年12月31 9:50
 * @email 1301584033@qq.com
 */
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling
public class ReadStartion {
    @Resource
    private StationMapper stationMapper;
    /**
     * @Author：
     * @Description：获取某个目录下所有直接下级文件，不包括目录下的子目录的下的文件，所以不用递归获取
     * @Date：
     */
    public  List<String> getFiles(String path) {
        List<String> files = new ArrayList<String>();
        File file = new File(path);
        File[] tempList = file.listFiles();

        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                if(tempList[i].getName().indexOf(".TXT")!=-1||tempList[i].getName().indexOf(".txt")!=-1){
                    files.add(tempList[i].toString());
                }

            }
        }
        return files;
    }
     //@Scheduled(cron = "0 0 8,20 * * ?")
    public  void Read(){
        String os = System.getProperty("os.name");
        String path="/meto/forcast";
        List<StationForcast> list=new ArrayList<>();
        if(os.toLowerCase().indexOf("windows")!=-1){
            path="D:/meto/forcast";
        }
        List<String> paths =getFiles(path);
        for(int i=0;i<paths.size();i++){
            StationForcast forcast=new StationForcast();
            FileReader fileReader = new FileReader(paths.get(i),"gbk");
            List<String> textLines= fileReader.readLines();
            String dateTime=textLines.get(3).trim().replace("SPCC","").trim();
            String tempNo = "";
            for(int j=5;j<textLines.size();j++){

                forcast.setDatetime(dateTime);
                String str[]=textLines.get(j).replaceAll(" +",",").split(",");
                if(str[0].equals("NNNN")){
                    break;
                }
                if((j-5)%15==0){
                    forcast=new StationForcast();
                    tempNo = str[0];
                    continue;
                }
                try {
                    if(Integer.valueOf(str[0])%24==0){
                        forcast.setStation_Id_C(tempNo);
                        forcast.setValidtime(str[0]);
                        forcast.setTEM_Max(str[11]);
                        forcast.setTEM_Min(str[12]);
                        forcast.setPRE(str[15]);
                        forcast.setWEP(str[19]);
                        forcast.setWIN_D(str[20]);
                        forcast.setWIN_S(str[21]);
                        list.add(forcast);
                        forcast=new StationForcast();
                    }
                }catch (Exception e){
                    System.out.println("------------------");
                    continue;
                }


            }
        }
        stationMapper.insertList(list);
    }
    //@Scheduled(fixedRate = 1000*60)
    public  void ReadFire(){
        String os = System.getProperty("os.name");
        String path="/meto/firemonitor";
        List<Firemonitor> list=new ArrayList<>();
        if(os.toLowerCase().indexOf("windows")!=-1){
            path="D:/meto/firemonitor";
        }
        String date=DateUtil.now().replace("-","").split(" ")[0];
        path+="/"+date;
        List<String> paths =getFiles(path);
        for(int i=0;i<paths.size();i++){
            Firemonitor firemonitor=new Firemonitor();
            FileReader fileReader = new FileReader(paths.get(i),"utf-8");
            List<String> textLines= fileReader.readLines();
            for(String value:textLines){
                firemonitor.setLon(value.substring(0,value.indexOf(",")));
                firemonitor.setLat(value.substring(value.indexOf(",")+1,value.indexOf(";")));
                String values[]= value.split(";");
                firemonitor.setTown(values[2]);
                firemonitor.setCity(values[1]);
                firemonitor.setCvalue(values[3]);
                firemonitor.setAreaCode(stationMapper.getAreaCodebYtownName(values[1]));
                firemonitor.setCdate(date);
            }
            list.add(firemonitor);
        }
        stationMapper.insertListFiremonitors(list);
    }




}
